<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
      <el-form-item label="关联ID" prop="refId">
        <el-input
          v-model="queryParams.refId"
          placeholder="请输入关联ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择任务类型" clearable size="small">
          <el-option label="诊所同步" :value="10" />
          <el-option label="处方同步" :value="20" />
          <el-option label="杂项同步" :value="30" />
          <el-option label="发货单同步" :value="40" />
          <el-option label="退货同步" :value="50" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable size="small">
          <el-option label="未执行" :value="0" />
          <el-option label="执行中" :value="1" />
          <el-option label="执行成功" :value="2" />
          <el-option label="执行失败" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="就诊号" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入就诊号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行次数" prop="execCount">
        <el-input
          v-model="queryParams.execCount"
          placeholder="请输入执行次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="valid">
        <el-select v-model="queryParams.valid" placeholder="请选择是否有效" clearable size="small">
          <el-option label="无效" :value="0" />
          <el-option label="有效" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:task:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="关联ID" align="center" prop="refId" />
      <el-table-column label="任务类型" align="center" prop="type">
        <template slot-scope="scope">
          <div>{{ handleType(scope.row.type) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <div>{{ handleStatus(scope.row.status) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="就诊号" align="center" prop="content" />
      <el-table-column label="执行次数" align="center" prop="execCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否有效" align="center" prop="valid">
        <template slot-scope="scope">
          <span v-if="scope.row.valid === 0">无效</span>
          <span v-else>有效</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type === 40 || scope.row.type === 50"
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['order:task:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:task:edit']"
            >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改AdEcom任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联ID" prop="refId">
          <el-input v-model="form.refId" placeholder="请输入关联ID" />
        </el-form-item>
        <el-form-item label="任务类型">
          <el-select v-model="form.type" placeholder="请选择任务类型">
            <el-option label="诊所同步" :value="10" />
            <el-option label="处方同步" :value="20" />
            <el-option label="杂项同步" :value="30" />
            <el-option label="发货单同步" :value="40" />
            <el-option label="退货同步" :value="50" />
          </el-select>
        </el-form-item>
        <el-form-item label="任务状态">
          <el-select v-model="form.status" placeholder="请选择任务状态" clearable size="small">
            <el-option label="未执行" :value="0" />
            <el-option label="执行中" :value="1" />
            <el-option label="执行成功" :value="2" />
            <el-option label="执行失败" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="任务内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="执行次数" prop="execCount">
          <el-input v-model="form.execCount" placeholder="请输入执行次数" />
        </el-form-item>
        <el-form-item label="是否有效" prop="valid">
          <el-select v-model="form.valid" placeholder="请选择是否有效" clearable size="small">
            <el-option label="无效" :value="0" />
            <el-option label="有效" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask, exportTask } from "@/api/order/task";

export default {
  name: "Task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // AdEcom任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refId: undefined,
        type: undefined,
        status: undefined,
        content: undefined,
        execCount: undefined,
        valid: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        refId: [
          { required: true, message: "关联ID不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "任务类型 10 订单拉取待同步任务 11 同步订单不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "任务状态不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "任务内容不能为空", trigger: "blur" }
        ],
        execCount: [
          { required: true, message: "执行次数不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        valid: [
          { required: true, message: "是否有效（0:无效；1:有效）不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询AdEcom任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        refId: undefined,
        type: undefined,
        status: undefined,
        content: undefined,
        execCount: undefined,
        remark: undefined,
        createTime: undefined,
        updateTime: undefined,
        valid: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 列表查看
    handleView(row) {
      this.$router.push(`/order/task/detail/${row.refId}`)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加AdEcom任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改AdEcom任务";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTask(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addTask(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除AdEcom任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有AdEcom任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTask(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    // 处理任务类型
    handleType(taskType) {
      let str = '';

      switch(taskType) {
        case 10:
          str = '诊所同步';
        break;
        case 20:
          str = '处方同步';
        break;
        case 30:
          str = '杂项同步';
        break;
        case 40:
          str = '发货单同步';
        break;
        case 50:
          str = '退货同步';
        break;
        default:
          str = '';
      }

      return str;
    },
    // 处理任务状态
    handleStatus(taskStatus) {
      let str = '';

      switch(taskStatus) {
        case 0:
          str = '未执行';
        break;
        case 1:
          str = '执行中';
        break;
        case 2:
          str = '执行成功';
        break;
        case 3:
          str = '执行失败';
        break;
        default:
          str = '';
      }

      return str;
    }
  }
};
</script>
